package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.enums.TableName;
import com.itms.mapper.SupplierMapper;
import com.itms.mapper.TableDataSyncMapper;
import com.itms.pojo.Package;
import com.itms.pojo.Supplier;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@EnableScheduling
public class SupplierJob {

    @Reference
    private BasicDataService basicDataService;
    @Autowired
    private SycService sycService;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private LogService logService;

    @Scheduled(initialDelayString = "${SupplierJob.initialDelayString}",fixedDelayString="${SupplierJob.fixedDelayString}" )
    public void deal(){
        try {
            List<Supplier> errorList = new ArrayList<>();

            //获取mdb表
            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBSupplier.toString());
            Date itms = this.sycService.getMaxSyncDate(TableName.MDBSupplier.toString());
            if (mdb==null) {
                return;
            }
            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) {
                return;
            }
                List<Supplier> list = basicDataService.getSupplierList(itms);

                for (Supplier s : list) {
                    com.itms.entity.Supplier supplier = supplierMapper.getSupplierBySupplierCode(s.getSupplierCode(),null);

                    if (supplier == null) {
                        supplier = new com.itms.entity.Supplier();
                        supplier.setSupplierCode(s.getSupplierCode());
                        supplier.setSupplierName(s.getSupplierName());
                        supplier.setIsValid(s.getIsValid());
                        supplier.setAddress(s.getAddress());
                        supplier.setCreateDate(new Date());
                        supplier.setLastUpdateDate(new Date());
                        supplier.setCreateUserId(new BigDecimal(9999));
                        supplierMapper.insertSelective(supplier);
                    } else {
                        supplier.setSupplierCode(s.getSupplierCode());
                        supplier.setSupplierName(s.getSupplierName());
                        supplier.setIsValid(s.getIsValid());
                        supplier.setAddress(s.getAddress());
                        supplier.setLastUpdateDate(new Date());
                        supplier.setLastUpdateUserId(new BigDecimal(9999));
                        supplierMapper.updateByPrimaryKeySelective(supplier);
                    }
                }

            if (errorList.size() == 0){
                this.sycService.save(mdb,TableName.MDBSupplier.toString());
            }
            this.logService.saveErrorList(errorList,TableName.MDBSupplier.toString(),this.getClass().getName());
        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e,TableName.MDBSupplier.toString(),this.getClass().getName());
        }
    }
}
